import ColorAnalysis from '../../../components/common/tui-color-analysis/tui-color-analysis.js'
const util = require("../../../utils/util.js")
import tui from '../../../common/httpRequest'
Page({
  data: {
    imgUrl: "",
    canvasWidth: 200,
    canvasHeight: 200,
    colorAnalysis: null,
    winWidth: 375,
    maxColors:5,
    colors: [],
    rbgColors:[]
  },
  onReady: function () {
    this.data.colorAnalysis = new ColorAnalysis('tui-color-palette');
    this.data.winWidth = wx.getSystemInfoSync().windowWidth;
  },
  chooseImg: function () {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        this.setData({
          imgUrl: res.tempFilePaths[0]
        })
        wx.getImageInfo({
          src: res.tempFilePaths[0],
          success: (imgInfo) => {
            let {
              width,
              height
            } = imgInfo;
            let scale = 0.6 * this.data.winWidth / Math.max(width, height);
            let canvasWidth = Math.floor(scale * width);
            let canvasHeight = Math.floor(scale * height);
            this.setData({
              canvasWidth: canvasWidth,
              canvasHeight: canvasHeight
            });
            this.data.colorAnalysis.getPalette({
              width: canvasWidth,
              height: canvasHeight,
              imgPath: res.tempFilePaths[0],
              maxColors: this.data.maxColors,
              step: 1
            }, (colorArr) => {
              if (colorArr) {
                let rbgColors=[]
                colorArr = colorArr.map((color) => {
                  rbgColors.push(`${color[0]},${color[1]},${color[2]}`)
                  return util.rgbToHex(color[0], color[1], color[2])
                })
                console.log(colorArr,rbgColors)
                this.setData({
                  colors: colorArr,
                  rbgColors:rbgColors
                })
              }
            });
          }
        })
      }
    })
  },
  setMaxColors(){
     this.setData({
       maxColors:4
     })
     tui.toast("设置成功，重新上传图片查看")
  },
  copyColor(e){
    let color=e.currentTarget.dataset.color;
    tui.copy(color,"颜色值已复制")

  },
  onShareAppMessage: function () {}
})